# Not run yet
import pandas as pd
import pickle as pkl
import numpy as np
import bidict
import os
from scipy.sparse import csr_matrix
import warnings
from scipy.sparse import (spdiags, SparseEfficiencyWarning, csc_matrix,
    csr_matrix, isspmatrix, dok_matrix, lil_matrix, bsr_matrix)
warnings.simplefilter('ignore', SparseEfficiencyWarning)

def daytoweek(x):
    if x > "2020-09-22": return 105
    elif x > "2020-09-15": return 104
    elif x > "2020-09-08": return 103
    elif x > "2020-09-15": return 102
    elif x > "2020-09-01": return 101
    elif x > "2020-08-25": return 100
    elif x > "2020-08-18": return 99
    elif x > "2020-08-11": return 98
    elif x > "2020-08-04": return 97
    elif x > "2020-07-28": return 96
    elif x > "2020-07-21": return 95
    else: return 0

n_artid = 105542
df = pd.read_csv(".\\input\\processed\\transactions_translated.csv", sep=',',index_col=0, usecols=[0, 1, 2, 3])
df.t_dat = df.t_dat.map(daytoweek)

cf = [None for i in range(105)]
for prev_week in range(95, 105, 1):
    fil = df.loc[df['t_dat'] == prev_week]
    a = csr_matrix((n_artid+1, n_artid+1), dtype=np.int32)
    for _, cf0 in fil.groupby('customer_id'):
        cf1 = pd.unique(cf0.article_id).tolist()
        ridx = []
        cidx = cf1 * len(cf1)
        for ele in cf1:
            ridx += [ele, ] * len(cf1)
        a[ridx, cidx] += 1
    cf[prev_week] = a
    print(prev_week, "Collaborative Filtering Done")

for prev_week in range(95, 105, 1):
    cf[prev_week].setdiag(0)
    mask = (cf[prev_week].data <= 2)
    cf[prev_week].data[mask] = 0
    cf[prev_week].eliminate_zeros()

cc = [None for i in range(105)]
for prev_week in range(95, 105, 1):
    cc[prev_week] = [[] for i in range(n_artid+1)]
    for r, c in zip(*cf[prev_week].nonzero()):
        cc[prev_week][r] += [(cf[prev_week][r, c], c), ]
    for i in range(n_artid+1):
        if (len(cc[prev_week][i]) <= 1): continue
        cc[prev_week][i].sort(reverse=True)
        cc[prev_week][i] = cc[prev_week][i][:5]
        # print(cc[prev_week][i])
    print(prev_week, "Co-click Pair Done")

with open('.\\input\\processed\\coclicks.pkl', 'wb') as f:
    pkl.dump(cc, f, pkl.HIGHEST_PROTOCOL)
    

